//---------------------------------------------------------------------------------
// Copyright (c) 2008 VoiceLynx Project
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
//     1. The origin of this software must not be misrepresented; you must not
//        claim that you wrote the original software. If you use this software
//        in a product, an acknowledgment in the product documentation would be
//        appreciated but is not required.
//
//     2. Altered source versions must be plainly marked as such, and must not be
//        misrepresented as being the original software.
//
//     3. This notice may not be removed or altered from any source
//        distribution.
//---------------------------------------------------------------------------------

#ifndef __VLCodec_h__
#define __VLCodec_h__

#include "VLPrerequisites.h"
#include "VLCircularBuffer.h"

#include "speex/speex.h"
#include "speex/speex_preprocess.h"




class VLSpeexDSP
{
public:
  VLSpeexDSP(VLU32 frameSize, VLU32 sampleRate);
  virtual ~VLSpeexDSP();

  bool process(VL16 *in, bool generateOutput = true);

private:
  SpeexPreprocessState *mState;
};

typedef std::auto_ptr<VLSpeexDSP> VLSpeexDSPPtr;










//////////////////// Base Classes //////////////////////
struct VLIEncoder
{
  virtual ~VLIEncoder() { }

  virtual VLByteArrayPtr encode(VLByteArrayPtr in) = 0;
};

struct VLIDecoder
{
  virtual ~VLIDecoder() { }

  virtual VLByteArrayPtr decode(VLByteArrayPtr in) = 0;
};

typedef std::auto_ptr<VLIEncoder> VLEncoderPtr;
typedef std::auto_ptr<VLIDecoder> VLDecoderPtr;

struct VLICodec
{
  virtual VLEncoderPtr createEncoder() = 0;
  virtual VLDecoderPtr createDecoder() = 0;
};

typedef std::auto_ptr<VLICodec>   VLCodecPtr;

////////////////////////// SPEEX ///////////////////////
class VLSpeexCodec : public VLICodec
{
public:
  VLSpeexCodec();

  VLEncoderPtr createEncoder();
  VLDecoderPtr createDecoder();

private:

};

class VLSpeexEncoder : public VLIEncoder
{
public:
  VLSpeexEncoder();
  virtual ~VLSpeexEncoder();

  VLByteArrayPtr encode(VLByteArrayPtr in);

private:
  VLSpeexDSPPtr mPreprocessor;

  // For encoding
  SpeexBits mEncodeBits;
  void *mEncodeState;

  // The data we get does not always come in the "correct" frame size - so we need
  // to buffer it here
  VLCircularBuffer mEncodeBuffer;
  VLCircularBuffer mToSendBuffer;

  // Temporary buffers we use frequently. mSpeexFrame length always equals mSpeexFrameSize.
  VLU32   mSpeexFrameSize;
  VLByte *mSpeexFrame;

  VLByte mLargeBuffer[512];
};

class VLSpeexDecoder : public VLIDecoder
{
public:
  VLSpeexDecoder();
  virtual ~VLSpeexDecoder();

  VLByteArrayPtr decode(VLByteArrayPtr);

private:

  // For decoding
  SpeexBits mDecodeBits;
  void *mDecodeState;

  // The data we get does not always come in the "correct" frame size - so we need
  // to buffer it here
  VLCircularBuffer mDecodeBuffer;

  // Temporary buffers we use frequently. mSpeexFrame length always equals mSpeexFrameSize.
  VLU32   mSpeexFrameSize;
  VLByte *mSpeexFrame;

  VLByte mLargeBuffer[512];
};

#endif